"""
交易 - 支付宝WAP支付相关模型
"""

from dataclasses import dataclass
from typing import Optional, Dict, Any
from jlpay.model.base import OrgBaseReq, BaseResponse


class WapPayRequest(OrgBaseReq):
    """支付宝WAP支付请求"""
    
    API_URL = "/open/trans/waph5pay"
    
    def __init__(self):
        """初始化支付宝WAP支付请求"""
        super().__init__()
        self.mch_id = None  # 商户号
        self.term_no = None  # 终端号
        self.pay_type = None  # 交易类型
        self.buyer_logon_id = None  # 买家支付宝账号
        self.buyer_id = None  # 买家支付宝用户ID
        self.limit_pay = None  # 指定支付方式
        self.out_trade_no = None  # 外部订单号
        self.payment_valid_time = None  # 订单有效期
        self.body = None  # 商品标题
        self.attach = None  # 商品描述
        self.total_fee = None  # 交易金额
        self.is_hire_purchase = None  # 分期标识 0：否 1：是
        self.hire_purchase_num = None  # 分期数
        self.hire_purchase_seller_percent = None  # 卖家承担的手续费比例
        self.remark = None  # 备注
        self.notify_url = None  # 回调地址
        self.op_user_id = None  # 操作员
        self.op_shop_id = None  # 门店号
        self.device_info = None  # 终端设备号
        self.mch_create_ip = None  # 终端IP
        self.longitude = None  # 经度
        self.latitude = None  # 纬度
        self.terminal_info = None  # 终端信息
    
    def set_mch_id(self, mch_id: str):
        """设置商户号"""
        self.mch_id = mch_id
        return self
    
    def set_term_no(self, term_no: str):
        """设置终端号"""
        self.term_no = term_no
        return self
    
    def set_pay_type(self, pay_type: str):
        """设置交易类型"""
        self.pay_type = pay_type
        return self
    
    def set_buyer_logon_id(self, buyer_logon_id: str):
        """设置买家支付宝账号"""
        self.buyer_logon_id = buyer_logon_id
        return self
    
    def set_buyer_id(self, buyer_id: str):
        """设置买家支付宝用户ID"""
        self.buyer_id = buyer_id
        return self
    
    def set_limit_pay(self, limit_pay: str):
        """设置指定支付方式"""
        self.limit_pay = limit_pay
        return self
    
    def set_out_trade_no(self, out_trade_no: str):
        """设置外部订单号"""
        self.out_trade_no = out_trade_no
        return self
    
    def set_payment_valid_time(self, payment_valid_time: str):
        """设置订单有效期"""
        self.payment_valid_time = payment_valid_time
        return self
    
    def set_body(self, body: str):
        """设置商品标题"""
        self.body = body
        return self
    
    def set_attach(self, attach: str):
        """设置商品描述"""
        self.attach = attach
        return self
    
    def set_total_fee(self, total_fee: str):
        """设置交易金额"""
        self.total_fee = total_fee
        return self
    
    def set_is_hire_purchase(self, is_hire_purchase: str):
        """设置分期标识"""
        self.is_hire_purchase = is_hire_purchase
        return self
    
    def set_hire_purchase_num(self, hire_purchase_num: str):
        """设置分期数"""
        self.hire_purchase_num = hire_purchase_num
        return self
    
    def set_hire_purchase_seller_percent(self, hire_purchase_seller_percent: str):
        """设置卖家承担的手续费比例"""
        self.hire_purchase_seller_percent = hire_purchase_seller_percent
        return self
    
    def set_remark(self, remark: str):
        """设置备注"""
        self.remark = remark
        return self
    
    def set_notify_url(self, notify_url: str):
        """设置回调地址"""
        self.notify_url = notify_url
        return self
    
    def set_op_user_id(self, op_user_id: str):
        """设置操作员"""
        self.op_user_id = op_user_id
        return self
    
    def set_op_shop_id(self, op_shop_id: str):
        """设置门店号"""
        self.op_shop_id = op_shop_id
        return self
    
    def set_device_info(self, device_info: str):
        """设置终端设备号"""
        self.device_info = device_info
        return self
    
    def set_mch_create_ip(self, mch_create_ip: str):
        """设置终端IP"""
        self.mch_create_ip = mch_create_ip
        return self
    
    def set_longitude(self, longitude: str):
        """设置经度"""
        self.longitude = longitude
        return self
    
    def set_latitude(self, latitude: str):
        """设置纬度"""
        self.latitude = latitude
        return self
    
    def set_terminal_info(self, terminal_info: Any):
        """设置终端信息"""
        self.terminal_info = terminal_info
        return self
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        data = super().to_dict()
        data.update({
            'mch_id': self.mch_id,
            'term_no': self.term_no,
            'pay_type': self.pay_type,
            'buyer_logon_id': self.buyer_logon_id,
            'buyer_id': self.buyer_id,
            'limit_pay': self.limit_pay,
            'out_trade_no': self.out_trade_no,
            'payment_valid_time': self.payment_valid_time,
            'body': self.body,
            'attach': self.attach,
            'total_fee': self.total_fee,
            'is_hire_purchase': self.is_hire_purchase,
            'hire_purchase_num': self.hire_purchase_num,
            'hire_purchase_seller_percent': self.hire_purchase_seller_percent,
            'remark': self.remark,
            'notify_url': self.notify_url,
            'op_user_id': self.op_user_id,
            'op_shop_id': self.op_shop_id,
            'device_info': self.device_info,
            'mch_create_ip': self.mch_create_ip,
            'longitude': self.longitude,
            'latitude': self.latitude,
            'terminal_info': self.terminal_info
        })
        return data


class WapPayResponse(BaseResponse):
    """支付宝WAP支付响应"""
    
    def __init__(self, response_dict=None):
        """
        初始化支付宝WAP支付响应
        
        Args:
            response_dict: 响应字典
        """
        super().__init__(response_dict)
        self.status = None  # 状态 1：待确认 3：失败
        self.mch_id = None  # 商户号
        self.term_no = None  # 终端号
        self.device_info = None  # 终端设备号
        self.transaction_id = None  # 平台订单号
        self.out_trade_no = None  # 外部订单号
        self.chn_transaction_id = None  # 渠道订单号
        self.total_fee = None  # 交易金额
        self.pay_info = None  # 支付信息
        self.order_time = None  # 订单时间
        self.pay_type = None  # 交易类型
        self.trans_time = None  # 交易时间
        
        if response_dict:
            self.status = response_dict.get('status')
            self.mch_id = response_dict.get('mch_id')
            self.term_no = response_dict.get('term_no')
            self.device_info = response_dict.get('device_info')
            self.transaction_id = response_dict.get('transaction_id')
            self.out_trade_no = response_dict.get('out_trade_no')
            self.chn_transaction_id = response_dict.get('chn_transaction_id')
            self.total_fee = response_dict.get('total_fee')
            self.pay_info = response_dict.get('pay_info')
            self.order_time = response_dict.get('order_time')
            self.pay_type = response_dict.get('pay_type')
            self.trans_time = response_dict.get('trans_time')
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        data = super().to_dict()
        data.update({
            'status': self.status,
            'mch_id': self.mch_id,
            'term_no': self.term_no,
            'device_info': self.device_info,
            'transaction_id': self.transaction_id,
            'out_trade_no': self.out_trade_no,
            'chn_transaction_id': self.chn_transaction_id,
            'total_fee': self.total_fee,
            'pay_info': self.pay_info,
            'order_time': self.order_time,
            'pay_type': self.pay_type,
            'trans_time': self.trans_time
        })
        return data 